package com.shuozi.keyboardpopup;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Button;

import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatImageView;

public class DrawableHorizontalButton extends AppCompatButton {

    public DrawableHorizontalButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas = getTopCanvas(canvas);
        super.onDraw(canvas);
    }

    private Canvas getTopCanvas(Canvas canvas) {
        Drawable[] drawables = getCompoundDrawables();
        if (drawables == null) {
            return canvas;
        }
        Drawable drawable = drawables[1];// 左面的drawable
        if (drawable == null) {
            drawable = drawables[2];// 右面的drawable
        }

        // float textSize = getPaint().getTextSize(); // 使用这个会导致文字竖向排下来

        if (drawable==null){
            return canvas;
        }
        Rect rect= drawable.getBounds();
        int drawH = rect.bottom;
        int draW = rect.right;


        setPadding(0, 0, 0, 0); // 直接贴到左边
        float dx = (getWidth() - draW) / 2;

        float dy = (getHeight() - drawH) /2;


         canvas.translate(0, dy);// 往右移动
        return canvas;
    }
}
